package com.agent.coreservice;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

/**
 * this class will get system information
 */
public class Configuration 
{

	private static final String REGQUERY_UTIL = "reg query ";
	private static final String REGSTR_TOKEN = "REG_SZ";

	public static String INSTALL_DIR = "C:/Program Files/LogiGear/TestArchitect/";
	
	private static final String TA_REG_INSTALL_DIR = REGQUERY_UTIL +
	"\"HKLM\\software\\JavaSoft\\Prefs\\com\\logigear\\ta\\ws\\control\" /v INSTALL_DIR";
	
	
	//04.09.2009 triet.to
	//add more reg key if we cannot find the default key on windows xp 64 bit
	private static final String TA_REG_INSTALL_DIR_XP_64 = REGQUERY_UTIL +
	"\"HKLM\\software\\Wow6432\\Node\\JavaSoft\\Prefs\\com\\logigear\\ta\\ws\\control\" /v INSTALL_DIR";
	
	
	
	//add reg key for windows server 2003 64 bit
	private static final String TA_REG_INSTALL_DIR_2003_SERVER_64 = REGQUERY_UTIL +
	"\"HKLM\\software\\Wow6432Node\\JavaSoft\\Prefs\\com\\logigear\\ta\\ws\\control\" /v INSTALL_DIR";
	
	public static final String UNIX_DEFAULT_INSTALL_FOLDER_ABS = "/usr/local/logigear/testarchitect";
	
	public static final String WINDOWS_DEFAULT_DATA_FOLDER = "C:/Documents and Settings/All Users/Application Data/LogiGear/TestArchitect";
	
	
	static final int OS_UNKNOWN		=0;
	static final int OS_WINDOW		=1;
	static final int OS_MAC			=2;
	static final int OS_UNIX		=3;
	
	static int osName=getOSName();
	
	/**
	 * get OS name
	 * @return
	 */
	static int getOSName(){
		int name=OS_UNKNOWN;
		String os = System.getProperty("os.name").toLowerCase();
		//windows
	    if(os.indexOf( "win" ) >= 0){
	    	name=OS_WINDOW;
	    }
	    else if(os.indexOf( "mac" ) >= 0){
	    	name=OS_MAC;
	    }
	    else if(os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0){
	    	name=OS_UNIX;
	    }
	    return name;
	    
	}
	
	/*
	 * check if OS is window
	 */
	public static boolean isWindows(){
	    return (osName==OS_WINDOW); 
 
	}
 
	/*
	 * check if OS is MAc
	 */
	public static boolean isMac(){
 
		return (osName==OS_MAC);
 
	}
 
	/*
	 * check if OS isUnix
	 */
	public static boolean isUnix(){
 
		return (osName==OS_UNIX);
 
	}

	/**
	 * Get install directory of TANG
	 * @param regKey registry key
	 * @return String contain path of install directory
	 */
	public static String getRegistryValueByKey(String regKey)
	{
		try {
			Process process = Runtime.getRuntime().exec(regKey);
			StreamReader reader = new StreamReader(process.getInputStream());
			reader.start();
			process.waitFor();
			reader.join();
			String result = reader.getResult();
			int p = result.indexOf(REGSTR_TOKEN);
			if (p == -1)
				return null;
			return result.substring(p + REGSTR_TOKEN.length()).trim();
		}
		catch (Exception e) {
			return null;
		}
	}
	
	
	public static String getLogFolderPath(){
		String javaHome = System.getProperty("java.home");
		String actionHanderDataPath = javaHome + File.separator + "lib" + File.separator + "ext" + File.separator + "log";
		return actionHanderDataPath;
	}
	
	public static String getUserHomePath(){
		String userHome = System.getProperty("user.home");
		return userHome;
	}
	
	public static String getLogPath(){
		return Configuration.getUserHomePath() + File.separator + "TAjavaAgent_log";
	}
	
	static class StreamReader extends Thread {
		private InputStream is;
		private StringWriter sw;

		StreamReader(InputStream is) {
			this.is = is;
			sw = new StringWriter();
		}

		public void run() {
			try {
				int c;
				while ((c = is.read()) != -1)
					sw.write(c);
			}
			catch (IOException e) { ; }
		}

		String getResult() {
			return sw.toString();
		}
	}
}
